<?php
//////////////////////  STAFF  MANAGE /////////////////////////
/*
Gets the html table to manage suppliers.
*/
function staffs_people_manage_table($people,$controller)
{
	$CI =& get_instance();
	$table='<table class="tablesorter" id="sortable_table">';
	
	$headers = array('<input type="checkbox" id="select_all" />',
	$CI->lang->line('common_last_name'),
	$CI->lang->line('common_first_name'),
	$CI->lang->line('staffs_id'),
	$CI->lang->line('staffs_position'),
	//$CI->lang->line('common_email'),
	$CI->lang->line('common_phone_number'),
	$CI->lang->line('common_modify')
	);
	
	$table.='<thead><tr>';
	foreach($headers as $header)
	{
		$table.="<th>$header</th>";
	}
	$table.='</tr></thead><tbody>';
	$table.=staffs_people_manage_table_data_rows($people,$controller);
	$table.='</tbody></table>';
	return $table;
}

/*
Gets the html data rows for the people.
*/
function staffs_people_manage_table_data_rows($people,$controller)
{
	$CI =& get_instance();
	$table_data_rows='';
	
	foreach($people->result() as $person)
	{
		$table_data_rows.=staffs_person_data_row($person,$controller);
	}
	
	if($people->num_rows()==0)
	{
		$table_data_rows.="<tr><td colspan='20'><div class='warning_message' style='padding:7px;'>".$CI->lang->line('common_no_persons_to_display')."</div></tr></tr>";
	}
	
	return $table_data_rows;
}

function staffs_person_data_row($person,$controller)
{
	$CI =& get_instance();
	$controller_name=$CI->uri->segment(1);
	$width = $controller->get_form_width();
	$width2 = $controller->get_form_width() + 350;

	$table_data_row='<tr>';
	$table_data_row.="<td width='3%'><input type='checkbox' id='person_$person->person_id' value='".$person->person_id."'/></td>";
	$table_data_row.='<td>'.ucfirst(strtolower(
character_limiter($person->last_name,13))).'</td>';
	$table_data_row.='<td>'.ucfirst(strtolower(character_limiter($person->first_name,13))).'</td>';
	$table_data_row.='<td>'.$person->account_number.'</td>';
	$table_data_row.='<td>'.ucwords(strtolower(character_limiter($person->position,20))).'</td>';
	//$table_data_row.='<td>'.mailto($person->email,character_limiter($person->email,22)).'</td>';
	$table_data_row.='<td>'.$person->phone_number.'</td>';		
	$table_data_row.='<td>'.anchor($controller_name."/view_profile/$person->person_id/width:$width", $CI->lang->line('staffs_more'),array('class'=>'thickbox','title'=>$CI->lang->line('staffs_profile'))).' &nbsp;&nbsp;'.anchor($controller_name."/view/$person->person_id/width:$width", $CI->lang->line('common_edit'),array('class'=>'thickbox','title'=>$CI->lang->line($controller_name.'_update'))).' &nbsp;&nbsp;'.anchor($controller_name."/open_account/$person->person_id/", $CI->lang->line('staffs_open_account'),array('class'=>'','title'=>$CI->lang->line('staffs_open'),'target'=>'blank')).' &nbsp;&nbsp;'.anchor($controller_name."/new_payroll_annex/$person->person_id/width:$width2", strtolower($CI->lang->line('staffs_new_pay_roll')),array('class'=>'thickbox','title'=>$CI->lang->line('staffs_new_pay_roll'))).'</td>';		
	$table_data_row.='</tr>';
	
	return $table_data_row;
}

/////////////////// For Reporting on Staff //////////////////////

function staffs_report_manage_table($people,$controller)
{
	$CI =& get_instance();
	$table='<table class="tablesorter" id="sortable_table">';
	
	$headers = array('<input type="checkbox" id="select_all" />',
	$CI->lang->line('common_last_name'),
	$CI->lang->line('common_first_name'),
	$CI->lang->line('staffs_id'),
	$CI->lang->line('staffs_position'),
	$CI->lang->line('common_email'),
	$CI->lang->line('common_phone_number')
	);
	
	$table.='<thead><tr>';
	foreach($headers as $header)
	{
		$table.="<th>$header</th>";
	}
	$table.='</tr></thead><tbody>';
	$table.=staffs_report_manage_table_data_rows($people,$controller);
	$table.='</tbody></table>';
	return $table;
}

/*
Gets the html data rows for the report.
*/
function staffs_report_manage_table_data_rows($people,$controller)
{
	$CI =& get_instance();
	$table_data_rows='';
	
	foreach($people->result() as $person)
	{
		$table_data_rows.=staffs_report_data_row($person,$controller);
	}
	
	if($people->num_rows()==0)
	{
		$table_data_rows.="<tr><td colspan='6'><div class='warning_message' style='padding:7px;'>".$CI->lang->line('common_no_persons_to_display')."</div></tr></tr>";
	}
	
	return $table_data_rows;
}

function staffs_report_data_row($person,$controller)
{
	$CI =& get_instance();
	$controller_name=$CI->uri->segment(1);

	$table_data_row='<tr>';
	$table_data_row.="<td width='3%'><input type='checkbox' id='person_$person->person_id' value='".$person->person_id."'/></td>";
	$table_data_row.='<td>'.ucfirst(strtolower(character_limiter($person->last_name,13))).'</td>';
	$table_data_row.='<td>'.ucfirst(strtolower(character_limiter($person->first_name,13))).'</td>';
	$table_data_row.='<td>'.$person->account_number.'</td>';
	$table_data_row.='<td>'.ucwords(strtolower(character_limiter($person->position,20))).'</td>';
	$table_data_row.='<td>'.mailto($person->email,character_limiter($person->email,22)).'</td>';
	$table_data_row.='<td>'.$person->phone_number.'</td>';				
	$table_data_row.='</tr>';
	
	return $table_data_row;
}


//////////////////// Managing of Payrolls///////////////////////////

/*
Gets the html table to manage items.
*/
function manage_payroll_table($items,$controller)
{
	$CI =& get_instance();
	$table='<table class="tablesorter" id="sortable_table">';
	
	$headers = array('<input type="checkbox" id="select_all" />', 
	$CI->lang->line('date'),
	$CI->lang->line('staffs_basic_wage').' '.$controller->config->item('currency_inuse'),
	//$CI->lang->line('staffs_allowances').' '.$controller->config->item('currency_inuse'),
	//$CI->lang->line('staffs_net_wage').' '.$controller->config->item('currency_inuse'),
	$CI->lang->line('staffs_gross_pay').' '.$controller->config->item('currency_inuse'),
	//$CI->lang->line('staffs_basic_tax').' '.$controller->config->item('currency_inuse'),
	//$CI->lang->line('staffs_taxable_income').' '.$controller->config->item('currency_inuse'),
	//$CI->lang->line('staffs_deductions').' '.$controller->config->item('currency_inuse'),
	$CI->lang->line('staffs_net_pay').' '.$controller->config->item('currency_inuse'),
	//$CI->lang->line('staffs_total_deductions').' '.$controller->config->item('currency_inuse'),
	$CI->lang->line('staffs_total_ssf_payable').' '.$controller->config->item('currency_inuse'),
	$CI->lang->line('common_modify')
	);
	
	$table.='<thead><tr>';
	foreach($headers as $header)
	{
		$table.="<th>$header</th>";
	}
	$table.='</tr></thead><tbody>';
	$table.=manage_payroll_table_data_rows($items,$controller);
	$table.='</tbody></table>';
	return $table;
}

/*
Gets the html data rows for the items.
*/
function manage_payroll_table_data_rows($items,$controller)
{
	$CI =& get_instance();
	$table_data_rows='';
	
	foreach($items as $sort)
	{
		$table_data_rows.=manage_payroll_item_data_row($sort,$controller);
	}				
	
	if(empty($items))
	{
		$table_data_rows.="<tr><td colspan='20'><div class='warning_message' style='padding:7px;'>".$CI->lang->line('items_no_items_to_display')."</div></tr></tr>";
	}
	return $table_data_rows;
}

function manage_payroll_item_data_row($item,$controller)
{
	$CI =& get_instance();
	$controller_name=$CI->uri->segment(1);
	$item_id = $item[0];

	$width = $controller->get_form_width() + 350;
	$width2 = $controller->get_form_width();
	
	
	$table_data_row='<tr>';
	$table_data_row.="<td width='3%'><input type='checkbox' id='item_$item_id' value='".$item_id."'/></td>";
	$table_data_row.='<td>'.$item[1].'</td>';
	$table_data_row.='<td>'.number_format($item[2], 2, '.', '').'</td>';
	$table_data_row.='<td>'.number_format($item[3], 2, '.', '').'</td>';
	$table_data_row.='<td>'.number_format($item[4], 2, '.', '').'</td>';
	$table_data_row.='<td>'.number_format($item[5], 2, '.', '').'</td>';
	$table_data_row.='<td>'.anchor($controller_name."/detailed_payroll/$item_id/width:$width2", $CI->lang->line('common_more'),array('class'=>'thickbox','title'=>$CI->lang->line('common_detailed_info'))).' &nbsp;&nbsp;'.anchor($controller_name."/edit_payroll/$item_id/width:$width", $CI->lang->line('common_edit'),array('class'=>'thickbox','title'=>$CI->lang->line('staffs_edit_payroll'))).' &nbsp;&nbsp;'.anchor($controller_name."/print_payroll/$item_id/", $CI->lang->line('common_print'),array('class'=>'','title'=>$CI->lang->line('common_print'),'target'=>'blank')).'</td>';		
	$table_data_row.='</tr>';
	
	return $table_data_row;
}


//////////////////////  Editing Payroll Allowance and Deductions /////////////////////////
/*
Gets the html table to manage suppliers.
*/
function edit_allow_diduc_manage_table($people,$controller,$who)
{
	$CI =& get_instance();
	$table='<table class="tablesorter" id="sortable_table">';
	
	$headers = array(
	$CI->lang->line('common_description'),
	$CI->lang->line('common_amount').' '.$controller->config->item('currency_inuse'),
	$CI->lang->line('common_modify')
	);
	
	$table.='<thead><tr>';
	foreach($headers as $header)
	{
		$table.="<th>$header</th>";
	}
	$table.='</tr></thead><tbody>';
	$table.=edit_allow_diduc_table_data_rows($people,$controller,$who);
	$table.='</tbody></table>';
	return $table;
}

/*
Gets the html data rows for the people.
*/
function edit_allow_diduc_table_data_rows($people,$controller,$who)
{
	$CI =& get_instance();
	$table_data_rows='';
	
	foreach($people->result() as $person)
	{
		$table_data_rows.=edit_allow_diduc_data_row($person,$controller,$who);
	}
	
	if($people->num_rows()==0)
	{
		$table_data_rows.="<tr><td colspan='8'><div class='warning_message' style='padding:7px;'>".$CI->lang->line('common_empty')."</div><input type='hidden' name='hidden_$who' id='hidden_$who' value='0'/></tr></tr>";
	}
	else
	{
		$table_data_rows.="<tr><td colspan='8'><input type='hidden' name='hidden_$who' id='hidden_$who' value='1'/></td></tr>";
	}
	
	return $table_data_rows;
}

function edit_allow_diduc_data_row($person,$controller,$who)
{
	$CI =& get_instance();
	$controller_name=$CI->uri->segment(1);
	$width = $controller->get_form_width();
	if($who=='allowance')
	{
		$type_id = $person->allowance_id;
		$who_delete = 1;
	}
	else
	{
		$type_id = $person->deductions_id;
		$who_delete = 0;
	}
	
	
	$name = "<input type='text' name='".$who."_name[]' id='".$who."_name[]' value='".$person->name."'/>"; 
	$amount = "<input type='text' name='".$who."_amount[]' id='".$who."_amount[]' value='".$person->amount."'/>%<input type='hidden' name='".$who."_update_id[]' id='".$who."_update_id[]' value='".$type_id."'/>";
	$delete = "<span style='cursor:pointer;' onclick='delete_allow_diduc(".$type_id.",".$person->payroll_id.",".$who_delete.");'>Delete</span>";

	$table_data_row='<tr>';
	$table_data_row.='<td>'.$name.'</td>';
	$table_data_row.='<td>'.$amount.'</td>';
	$table_data_row.='<td>'.$delete.'</td>';	
	$table_data_row.='</tr>';
	
	return $table_data_row;
}


//////////////////////  Editing Payroll Allowance /////////////////////////
/*
Gets the html table to manage suppliers.
*/
function edit_allow_manage_table($people,$controller,$who)
{
	$CI =& get_instance();
	$table='<table class="tablesorter" id="sortable_table">';
	
	$headers = array(
	$CI->lang->line('common_description'),
	$CI->lang->line('common_amount').' '.$controller->config->item('currency_inuse'),
	$CI->lang->line('common_taxable'),
	$CI->lang->line('common_modify')
	);
	
	$table.='<thead><tr>';
	foreach($headers as $header)
	{
		$table.="<th>$header</th>";
	}
	$table.='</tr></thead><tbody>';
	$table.=edit_allow_table_data_rows($people,$controller,$who);
	$table.='</tbody></table>';
	return $table;
}

/*
Gets the html data rows for the people.
*/
function edit_allow_table_data_rows($people,$controller,$who)
{
	$CI =& get_instance();
	$table_data_rows='';
	$cunt = 1;
	foreach($people->result() as $person)
	{
		$table_data_rows.=edit_allow_data_row($person,$controller,$who,$cunt);
		$cunt++;
	}
	
	if($people->num_rows()==0)
	{
		$table_data_rows.="<tr><td colspan='8'><div class='warning_message' style='padding:7px;'>".$CI->lang->line('common_empty')."</div><input type='hidden' name='hidden_$who' id='hidden_$who' value='0'/></tr></tr>";
	}
	else
	{
		$table_data_rows.="<tr><td colspan='8'><input type='hidden' name='hidden_$who' id='hidden_$who' value='1'/></td></tr>";
	}
	
	return $table_data_rows;
}

function edit_allow_data_row($person,$controller,$who,$cunt)
{
	$CI =& get_instance();
	$controller_name=$CI->uri->segment(1);
	$width = $controller->get_form_width();
	if($who=='allowance')
	{
		$type_id = $person->allowance_id;
		$who_delete = 1;
	}
	else
	{
		$type_id = $person->deductions_id;
		$who_delete = 0;
	}
	
	if($person->taxable == 1)
	{
		$checked_str = "checked='checked'";
	}
	else
	{
		$checked_str = "";
	}
	
	$name = "<input type='text' name='".$who."_name[]' id='".$who."_name[]' value='".$person->name."'/>"; 
	$amount = "<input type='text' name='".$who."_amount[]' id='".$who."_amount[]' value='".$person->amount."'/>%<input type='hidden' name='".$who."_update_id[]' id='".$who."_update_id[]' value='".$type_id."'/>";
	$taxable = "<input type='checkbox' name='taxable".$cunt."' value='1' $checked_str/>"; 
	$delete = "<span style='cursor:pointer;' onclick='delete_allow_diduc(".$type_id.",".$person->payroll_id.",".$who_delete.");'>Delete</span>";

	$table_data_row='<tr>';
	$table_data_row.='<td>'.$name.'</td>';
	$table_data_row.='<td>'.$amount.'</td>';
	$table_data_row.='<td>'.$taxable.'</td>';
	$table_data_row.='<td>'.$delete.'</td>';	
	$table_data_row.='</tr>';
	
	return $table_data_row;
}

?>